package com.xpert.action.board;

import java.io.File;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.xpert.action.CommandAction;
import com.xpert.beans.Board;
import com.xpert.common.Constants;
import com.xpert.dao.BoardDao;

public class DeleteAction implements CommandAction {

    @Override
    public String requestProc(HttpServletRequest request,
    		HttpServletResponse response) throws Throwable {

    	int idx = Integer.parseInt(request.getParameter("idx"));
    	Board board = BoardDao.getInstance().getArticle(idx);
    	String fileName = board.getFileName();
    	String uploadFileName = 
    			request.getSession().getServletContext()
    			.getRealPath(Constants.FILE_SAVE_DIR) + Constants.DIR_SEPARATOR_WIN + fileName;

    	File uploadFile = new File(uploadFileName);
    	
    	if(uploadFile.exists() && uploadFile.isFile()) {
    		uploadFile.delete();
    	}

    	BoardDao.getInstance().deleteArticle(idx);
    
    	String page = request.getParameter("page");
    	request.setAttribute("url", "list.do?page=" + page);

    	return "redirect.jsp";
    }

}
